package com.zr.kucunkufangbiaoshi.controller.service.impl;


import com.zr.kucunkufangbiaoshi.controller.mapper.WareHouseIdentifyMapper;
import com.zr.kucunkufangbiaoshi.controller.meiju.StatusEnum;
import com.zr.kucunkufangbiaoshi.controller.pojo.*;
import com.zr.kucunkufangbiaoshi.controller.service.WareHouseIdentifyService;
import com.zr.kucunkufangbiaoshi.controller.util.AllRecords;
import com.zr.kucunkufangbiaoshi.controller.util.ResultBuildVo;
import com.zr.kucunkufangbiaoshi.controller.util.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**
 * Created by Administrator on 2019/7/16 0016.
 */
@Service
public class WareHouseIdentifyServiceImpl implements WareHouseIdentifyService {
    @Autowired
    private WareHouseIdentifyMapper wareHouseIdentifyMapper;
    @Override
    public ResultVo<AllRecords> queryPage(WareHouseIdentifySelectVo wareHouseIdentifySelectVo) {
        //查询数据
        List<WareHouseIdentify> wareHouseIdentifyList = wareHouseIdentifyMapper.queryPage(wareHouseIdentifySelectVo);
        //查询数量
        int count = wareHouseIdentifyMapper.queryCount(wareHouseIdentifySelectVo);
        AllRecords allRecords = new AllRecords();
        allRecords.setPageIndex(wareHouseIdentifySelectVo.getPageIndex());
        allRecords.setPageSize(wareHouseIdentifySelectVo.getPageSize());
        allRecords.setTotalNumber(count);
        allRecords.resetTotalNumber(count);
        allRecords.setDataList(wareHouseIdentifyList);
        return ResultBuildVo.success(allRecords);

    }

    @Override
    @Transactional
    public ResultVo add(WareHouseIdentifyAddVo wareHouseIdentifyAddVo) {

        WareHouseIdentify wareHouseIdentify = new WareHouseIdentify();
        Date nowDate = new Date();

        wareHouseIdentify.setCreateTime(nowDate);
        wareHouseIdentify.setCreateName("小明");
        wareHouseIdentify.setUpdateTime(nowDate);
        wareHouseIdentify.setUpdateName("小明");
        BeanUtils.copyProperties(wareHouseIdentifyAddVo,wareHouseIdentify);
        wareHouseIdentifyMapper.add(wareHouseIdentify);
        return ResultBuildVo.success(wareHouseIdentifyAddVo);
    }

    @Override
    public ResultVo updateStatus(WareHouseIdentifyStatusVo wareHouseIdentifyStatusVo) {
        WareHouseIdentify wareHouseIdentify = new WareHouseIdentify();
        Date nowDate = new Date();
        wareHouseIdentify.setUpdateTime(nowDate);
        wareHouseIdentify.setUpdateName("花花");
        BeanUtils.copyProperties(wareHouseIdentifyStatusVo, wareHouseIdentify);
        wareHouseIdentifyMapper.updateStatus(wareHouseIdentify);
        return ResultBuildVo.success(wareHouseIdentifyStatusVo);
    }

    @Override
    @Transactional
    public ResultVo update(WareHouseIdentifyUpdateVo wareHouseIdentifyUpdateVo) {
        //判断是否为禁用，禁用可以修改
        ResultVo assigment = assigmentUpdate(wareHouseIdentifyUpdateVo);
        if (!assigment.isSuccess()) {
            return assigment;
        }
        //给数据库中的创建时间和修改时间等信息赋值
        WareHouseIdentify wareHouseIdentify = new WareHouseIdentify();
        Date nowDate = new Date();
        wareHouseIdentify.setUpdateTime(nowDate);
        wareHouseIdentify.setUpdateName("花花");
        BeanUtils.copyProperties(wareHouseIdentifyUpdateVo, wareHouseIdentify);
        wareHouseIdentifyMapper.update(wareHouseIdentify);
        return ResultBuildVo.success(wareHouseIdentifyUpdateVo);
    }
    private ResultVo assigmentUpdate(WareHouseIdentifyUpdateVo wareHouseIdentifyUpdateVo) {
        WareHouseIdentify wareHouseIdentify = wareHouseIdentifyMapper.queryById(wareHouseIdentifyUpdateVo.getId());
        if (wareHouseIdentify == null){
            return ResultBuildVo.error("修改数据不存在！","500");
        }
        //禁用状态下才允许修改
        if (wareHouseIdentify.getStatus() == StatusEnum.QIYONG.getStatusValue()){
            return ResultBuildVo.error("禁用状态才能修改！","500");
        }
        return ResultBuildVo.success(wareHouseIdentifyUpdateVo);
    }
}
